<?php

/**
 * uniBoard API language
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_language extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Language ID
	 *
	 * @var integer
	 */
	
	public $language_id 			= 0;
	
	/**
	 * Language Name
	 *
	 * @var string
	 */
	
	public $language_name			= '';
	
	/**
	 * Language Iso
	 *
	 * @var string
	 */
	
	public $language_iso	 		= '';
	
	/**
	 * Language Author
	 *
	 * @var string
	 */
	
	public $language_author 		= '';
	
	/**
	 * Language E-Mail
	 *
	 * @var string
	 */
	
	public $language_email	 		= '';
	
	/**
	 * Language WWW
	 *
	 * @var string
	 */
	
	public $language_www	 		= '';
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store lang id
		$this -> language_id 	= (integer) $target_id;
		
		// Autoload If Language ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'languages
			WHERE language_id = \'' . $this -> language_id . '\'
			LIMIT 1');
			
			// lang found
			if ( $language_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> language_name 				= $language_result['language_name'];
				$this -> language_iso	 			= $language_result['language_iso'];
				$this -> language_author 			= $language_result['language_author'];
				$this -> language_email 			= $language_result['language_email'];
				$this -> language_www 				= $language_result['language_www'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_language_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> language_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'languages');
			
				// Store new language id
				$this -> language_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'languages', 'language_id = \'' . $this -> language_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'languages');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set language_name
	 *
	 * @param string $new_value
	 */
	
	function setLanguageName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> language_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_language_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['language_name'] = $this -> appCore -> db -> strEscape( $this -> language_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_language_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set language_iso
	 *
	 * @param string $new_value
	 */
	
	function setlanguageIso( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> language_iso = $this -> appCore -> secureFilename( $new_value);
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_language_iso_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['language_iso'] = $this -> appCore -> db -> strEscape( $this -> language_iso);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_language_iso_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set language_author
	 *
	 * @param integer $new_value
	 */
	
	function setLanguageAuthor( $new_value){
		
		// Store in object
		$this -> language_author = $new_value;

		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_language_author_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['language_author'] = $this -> appCore -> db -> strEscape( $this -> language_author);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set language_email
	 *
	 * @param integer $new_value
	 */
	
	function setLanguageEmail( $new_value){

		// Store in object
		$this -> language_email = $new_value;
	
		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_language_email_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['language_email'] = $this -> appCore -> db -> strEscape( $this -> language_email);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set language_www
	 *
	 * @param integer $new_value
	 */
	
	function setLanguageWww( $new_value){

		// Store in object
		$this -> language_www = $new_value;
	
		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_language_www_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['language_www'] = $this -> appCore -> db -> strEscape( $this -> language_www);
		}
		
	}
		
}